package com.cndongtai.controller.torchn;

import java.text.DecimalFormat;
import java.util.Date;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cndongtai.controller.BaseController;
import com.cndongtai.po.TorchnBatteryCollectRecord;
import com.cndongtai.po.TorchnInverterCollectRecord;
import com.cndongtai.po.TorchnUserInfo;
import com.cndongtai.service.torchn.TorchnLoginService;
import com.cndongtai.service.torchn.TorchnService;
import com.cndongtai.util.DateUtil;
import com.cndongtai.util.ValidateUtil;

@RequestMapping("/torchn")
@Controller
public class TorchnController extends BaseController{
	
	@Resource(name="torchnLoginService")
	private TorchnLoginService torchnLoginService;
	
	@Resource(name = "torchnService")
	private TorchnService torchnService;
	
	@RequestMapping("/login")
	@ResponseBody
	public void torchnLogin() {
		JSONObject result = new JSONObject();
		
		String userInfoStr = this.getValue("userInfo");
//		String code = this.getValue("code");
		
		try {
			JSONObject userInfo = JSONObject.fromObject(userInfoStr);
			
			TorchnUserInfo user = new TorchnUserInfo();
			user.setNickName(userInfo.getString("nickName"));
			user.setGender(userInfo.getInt("gender"));
			user.setCity(userInfo.getString("city"));
			user.setProvince(userInfo.getString("province"));
			user.setCountry(userInfo.getString("country"));
			user.setLanguage(userInfo.getString("language"));
			user.setAvatarUrl(userInfo.getString("avatarUrl"));
			user.setCreateTime(new Date());
			
			if(torchnLoginService.addTorchnUserInfo(user)) {
				result.put("code", "00");
				result.put("msg", "success");	
			}else {
				result.put("code", "10");
				result.put("msg", "fail");
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			result.put("code", "11");
			result.put("msg", "success");
		}
		
		this.writeData(result);
	}
	
	@RequestMapping("/saveInverterCollect")
	@ResponseBody
	public void saveInverterCollect(TorchnInverterCollectRecord record) {
		JSONObject result = new JSONObject();
		
		try {
			
			log.error("TorchnInverterCollectRecord==>"+record.toString());

			if(ValidateUtil.empty(record.getDeviceName())) {
				result.put("code", "0001");
				result.put("msg", "设备名称不能为空");
				this.writeData(result);
				return;
			}
			
			if(ValidateUtil.notEmpty(record.getCollectTimeStr())) {
				record.setCollectTime(DateUtil.parseDateFull(record.getCollectTimeStr()));
			}else {
				result.put("code", "0001");
				result.put("msg", "采集时间格式错误");
				this.writeData(result);
				return;
			}
			
//			record.setCollectTime(new Date());
			
			if(torchnService.addTorchnInverterCollectRecord(record)) {
				result.put("code", "0000");
				result.put("msg", "success");	
			}else {
				result.put("code", "0001");
				result.put("msg", "fail");
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			result.put("code", "0001");
			result.put("msg", "数据插入失败");
		}
		
		this.writeData(result);
	}
	
	@RequestMapping("/saveQuantity")
	@ResponseBody
	public void saveQuantity() {
		JSONObject result = new JSONObject();
		
		String deviceName = this.getValue("deviceName");
		String batterygroupvol = this.getValue("batterygroupvol");
		String batterygroupcurrent = this.getValue("batterygroupcurrent");
		String timeinterval = this.getValue("timeinterval");
		
		String voltagecore1 = this.getValue("voltagecore1");
		String voltagecore2 = this.getValue("voltagecore2");
		String voltagecore3 = this.getValue("voltagecore3");
		String voltagecore4 = this.getValue("voltagecore4");
		
		
		try {
			
			
			TorchnBatteryCollectRecord record = new TorchnBatteryCollectRecord();
			
			record.setDeviceName(deviceName);
			record.setBatteryVol(Double.valueOf(batterygroupvol));
			record.setBatteryCurrent(Double.valueOf(batterygroupcurrent));
			record.setTimeInterval(Integer.valueOf(timeinterval));
			record.setCollectTime(new Date());
			
			record.setVoltagecore1(ValidateUtil.empty(voltagecore1)?0:Double.valueOf(voltagecore1));
			record.setVoltagecore2(ValidateUtil.empty(voltagecore2)?0:Double.valueOf(voltagecore2));
			record.setVoltagecore3(ValidateUtil.empty(voltagecore3)?0:Double.valueOf(voltagecore3));
			record.setVoltagecore4(ValidateUtil.empty(voltagecore4)?0:Double.valueOf(voltagecore4));
			
			if(torchnService.addTorchnBatteryCollectRecord(record)) {
				result.put("code", "00");
				result.put("msg", "success");	
			}else {
				result.put("code", "10");
				result.put("msg", "fail");
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			result.put("code", "11");
			result.put("msg", "success");
		}
		
		this.writeData(result);
	}
	
	
	@RequestMapping("/queryChargeQuantity")
	@ResponseBody
	public void queryChargeQuantity() {
		JSONObject result = new JSONObject();
		
		String deviceName = this.getValue("deviceName");
		
		try {
			double quantity = torchnService.queryBatteryChargeQuantity(deviceName);
			
			DecimalFormat format = new DecimalFormat("#.######");
			
			result.put("code", "00");
			result.put("msg", "success");
			result.put("quantity", format.format(Math.abs(quantity)/3600/1000));
			
		}catch(Exception e) {
			e.printStackTrace();
			result.put("code", "11");
			result.put("msg", "success");
		}
		
		this.writeData(result);
	}
	
	@RequestMapping("/queryWorkQuantity")
	@ResponseBody
	public void queryWorkQuantity() {
		JSONObject result = new JSONObject();
		
		String deviceName = this.getValue("deviceName");
		
		try {
			double quantity = torchnService.queryBatteryWorkQuantity(deviceName);
			DecimalFormat format = new DecimalFormat("#.######");
			
			result.put("code", "00");
			result.put("msg", "success");
			result.put("quantity", format.format(Math.abs(quantity)/3600/1000));
			
		}catch(Exception e) {
			e.printStackTrace();
			result.put("code", "11");
			result.put("msg", "success");
		}
		
		this.writeData(result);
	}

}
